/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.cdinfusion.data.CDElement;
import org.cdinfusion.data.GetInformation;
import org.cdinfusion.data.environment.Environment;
import org.cdinfusion.data.studytype.StudyType;
import org.cdinfusion.filehandling.CSVHandler;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class GSC_a_Controller extends AbstractController {

	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("##### GSC_a_Controller #####");

		// TODO: Exception handling
		final HttpSession session = request.getSession();
		final String origin = (String) session.getAttribute("origin");
		final String studytype = request.getParameter("StudyTypeDropDown");
		final String envtype = request.getParameter("EnvParamDropDown");
		final String sessionID = request.getSession().getId();
		final String button = request.getParameter("submit");

		session.setAttribute("opt_2b", "one");

		final List<CDElement> cdeLIST = new ArrayList<CDElement>();
		@SuppressWarnings("unchecked")
		final List<String> seqIDs = (List<String>) session
				.getAttribute("allSequenceIDs");

		StudyType study = null;
		Environment env = null;
		
		try {
			study = (StudyType) Class.forName("org.cdinfusion.data.studytype." + studytype.toUpperCase()).newInstance();
		} catch (final Exception e) {
			System.out.println(e);
			System.out.printf("Could not create StudyType object for %s.", studytype);
		}
		try {
			env = (Environment)Class.forName("org.cdinfusion.data.environment." + StringUtils.capitalize(envtype)).newInstance();
		} catch (final Exception e) {
			System.out.println(e);
			System.out.printf("Could not create Environment object for %s.", envtype);
		}
		
		final int numberOfSequences = ((Integer) session
				.getAttribute("numberOfSequencesUploaded")).intValue();
		final CDElement cdelement = new CDElement(seqIDs.get(0), seqIDs.get(0),
				numberOfSequences, study, env, null, false);
		cdeLIST.add(cdelement);
		session.setAttribute("cdeLIST", cdeLIST);

		if (envtype.equals("none") || studytype.equals("none") || study == null || env == null) {
			ModelAndView errmav = new ModelAndView("ErrorPage");
			errmav.addObject("origin", origin);
			return errmav;
		}

		if (button.contains("download empty csv")) {
			CSVHandler csvh = new CSVHandler();
			csvh.generateEmptyCSV(studytype, envtype, sessionID, seqIDs);
			ModelAndView mav = new ModelAndView("CSV_CREATION");
			mav.addObject("origin", origin);
			return (mav);
			/*
			 * generate empty csv (sessionID.csv) in data directory with chosen
			 * "report" and "env" fields to generate the csv file, use the
			 * CSVHandler (org.cdinfusion.filehandling.CSVHandler) create
			 * ModelAndView with target "CSV_CREATION.jsp" and add "report" and
			 * "env" selection
			 */
		}

		final String[][] envAttributes = GetInformation.nameInformation(env);
		final String[][] studyAttributes = GetInformation.nameInformation(study);

		ModelAndView mav = new ModelAndView("3a_CD_INPUT_1to1");
		mav.addObject("origin", origin);
		mav.addObject("envAttributes", envAttributes);
		mav.addObject("studyAttributes", studyAttributes);
		mav.addObject("studyName", GetInformation.getLongNameForStudy(study));
		mav.addObject("study", studytype);
		mav.addObject("environment", envtype);
		mav.addObject("environmentName", GetInformation.getLongNameForEnvironment(env));
		
		return mav;
	}
}
